<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Ad extends Base {

    /**
     * @description : [广告列表]
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index(){
        $list = Db::table('bze_ad')->select();
        return $this->fetch('', [
            'list'  =>  $list,
        ]);
    }

    /**
     * @description : [广告 - 添加/编辑]
     * @return mixed
     */
    public function add(){
        if(request()->isPost()){
            $postData = input('post.');
            $location_id = $postData['location_id'];
            if('1'==$location_id){
                model('Ad')->add($postData);
                $this->success('添加成功', 'ad/index');
            }else{
                //
                $count = model('Ad')->where(['location_id'=>$location_id])->count();
                if($count==0){
                    model('Ad')->add($postData);
                }else{
                model('Ad')->where(['location_id'=>$location_id])->update($postData);
                }
                $this->success('添加成功', 'ad/index');
            }
        }else{
            return $this->fetch();
        }
    }

    /**
     * @description : [广告 - 删除]
     */
    public function delete(){
        $postData = input('post.');
        $id = $postData['id'];
        $res = model('Ad')->where(['id'=>$id])->delete();
        if(false !== $res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }

    }

    /**
     * @description : [视频列表]
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function videoIndex(){
        $where['group_key'] = ['IN', ['VIDEO','VIDEO_IMG']];
        $list = model('Config')->where($where)->select();
        $newList = [];
        foreach($list AS $key=>$val){
            if('_IMG'!=substr($val['config_key'], -4)){
                foreach($list AS $key2=>$val2){
                    if($val2['config_key'] == $val['config_key'].'_IMG'){
                        $list[$key]['config_value_img'] = $val2['config_value'];
                        array_push($newList, $list[$key]);
                        continue;
                    }
                }
            }
        }
        return $this->fetch('', [
            'list'  =>  $newList,
        ]);
    }

    /**
     * @description : [编辑视频]
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function videoEdit(){
        $input = input('param.');
        $id = $input['id'];
        $config = model('Config')->find($id);
        $configImage = model('Config')->where(['config_key'=>$config['config_key'].'_IMG'])->find();
        $config['config_image'] = $configImage['config_value'];
        if(!request()->isPost()){
            return $this->fetch('', [
                'config' => $config,
            ]);
        }else{
            $config_image = $input['config_image'];
            $config_video = $input['config_video'];
            model('Config')
                ->save(['config_value'=>$config_video],['id'=>$id]);
            model('Config')
                ->save(['config_value'=>$config_image],['config_key'=>$config['config_key'].'_IMG']);
            return show(0, '编辑成功');
        }
    }

}
